package compile;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {
    public static String readFile(String filePath){
//        FileInputStream inputStream=new FileInputStream(filePath);
//        String str="";
//        try {
//            while (true){
//                int ch=inputStream.read();
//                if (ch==-1){
//                    break;
//                }
//                str+=ch;
//            }
//        } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        return str;
            StringBuffer buffer=new StringBuffer();
            try (FileReader fileReader=new FileReader(filePath)){
                while(true){
                    int ch=fileReader.read();
                    if (ch==-1){
                        break;
                    }
                    buffer.append((char)ch);
                }
                fileReader.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return buffer.toString();
    }
    public static void writeFile(String filePath,String content){
        try(FileWriter fileWriter=new FileWriter(filePath)){
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FileUtil.writeFile("d:/test.txt","hello world");
        String content=FileUtil.readFile("d:/test.txt");
        System.out.println(content);
    }
}
